<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>姓名案例-计算属性实现</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        姓：<input type="text" v-model="firstname"><br/>
        名：<input type="text" v-model="lastname"><br/>
        全名：<span>{{fullname}}</span>
    </div>
</body>

<script>
    new Vue({
        el:'#root',
        data:{
          firstname:'张',
          lastname:'三',
          fullname:'张-三'
        },
        /*computed:{
            fullname(){
                return this.firstname+'-'+this.lastname;
            }
        },*/
        watch:{
            //定时器控制的函数不是vue管理的，时间到了是服务器来调用的，所以需要用箭头函数
            firstname(newValue,oldValue){
                setTimeout(()=>{
                    this.fullname=newValue+'-'+this.lastname
                },1000)
            },
            lastname(newValue,oldValue){
                this.firstname=this.firstname+'-'+newValue
            }
        }   
    })
</script>

</html>